package fr.mabdylon.gamexperience.launchers;

import processing.core.PApplet;
import processing.core.PVector;
import fr.mabdylon.gamexperience.core.Pulsar;
import fr.mabdylon.gamexperience.particles.Ball;

public class MainLauncher {}
	/*
	 * TODO : Creer un launcher pour gerer conditions de depart et multipulsar
	 * 
	 * public static final Pulsar pulsesWorld = new Pulsar();
	 *

	public MainLauncher() {
	}

	// Demarrage
	public static void main(String args[]) {
//		MainLauncher main = new MainLauncher();
		
		PApplet.main(new String[] { "--present",
				"fr.mabdylon.gamexperience.launchers.MainLauncher" });
	}

	// Generateur de balle avec copie de tableazu
	public Ball generateRandomBall() {
		int type = (int) random(pulsesWorld.TYPE_RANDOM_MATIERE_DEBUT,
				pulsesWorld.TYPE_RANDOM_MATIERE_FIN);
		return new Ball(random(1500), random(900),
				random(pulsesWorld.MIN_RANDOM_SIZE,
						pulsesWorld.MAX_RANDOM_SIZE), type);
	}

	public PVector generateRandomVel() {
		return new PVector((float) random(pulsesWorld.MIN_RANDOM_VELOCITY,
				pulsesWorld.MAX_RANDOM_VELOCITY),
				(float) random(pulsesWorld.MAX_RANDOM_VELOCITY));
	}

	public void loadSetup(int nb_balls, int max_width_size,
			int max_height_size, int initial_nb_balls) {
		// TODO : implementer client serveur
		// myServer = new Server(this, 5204);
		PopulateWorld(initial_nb_balls);
		size(800, 600);
		noStroke();
		smooth();
		// TODO : opengl
		// size(max_width_size, max_height_size,OPENGL);
		// hint(ENABLE_OPENGL_4X_SMOOTH);
	}

	public void reset(int nb_balls, int max_width_size, int max_height_size,
			int initial_nb_balls) {
		pulsesWorld.balls = new Ball[0];
		pulsesWorld.vels = null;
		loadSetup(nb_balls, max_width_size, max_height_size, initial_nb_balls);
		// Kestcekecafait ????
		// stroke(0.2f,0.5f,0.3f);
		noStroke();
	}

	public void setup() {
		loadSetup(pulsesWorld.INITIAL_NB_BALLS,
				pulsesWorld.INITIAL_MAX_WIDTH_SIZE,
				pulsesWorld.INITIAL_MAX_HEIGHT_SIZE,
				pulsesWorld.INITIAL_NB_BALLS);
	}

	public void PopulateWorld(int nbBalls) {
		pulsesWorld.balls = new Ball[nbBalls];
		pulsesWorld.vels = new PVector[nbBalls];
		for (int i = 0; i < nbBalls; i++) {
			pulsesWorld.balls[i] = generateRandomBall();
			pulsesWorld.vels[i] = generateRandomVel();
		}
	}

	public void draw() {
		background(color(30, 30, 30));
		for (int i = 0; i < pulsesWorld.balls.length; i++) {
			if (pulsesWorld.balls[i] != null) {
				pulsesWorld.balls[i].x += pulsesWorld.vels[i].x;
				pulsesWorld.balls[i].y += pulsesWorld.vels[i].y;
				ellipse(pulsesWorld.balls[i].x, pulsesWorld.balls[i].y,
						pulsesWorld.balls[i].r * 2,
						pulsesWorld.balls[i].r * 2);
				fill(pulsesWorld.balls[i].getColor());
				// Check les collisions avec les murs
				pulsesWorld.checkBoundaryCollision(pulsesWorld.balls[i],
						pulsesWorld.vels[i]);
			}
		}
		if (pulsesWorld.balls != null) {
			// Check les collisions entre les balles
			pulsesWorld.checkObjectCollision(pulsesWorld.balls,
					pulsesWorld.vels);

		}

		// Condition fin de jeu, on relance
		// TODO : finir de debugger
		/*
		 * boolean finDePartie = false; int typeFinMatiere = 0; int
		 * typeFinAntiMatiere = 1; for (int i = 0; i < balls.length; i++) { if
		 * (balls[i].t == 0) typeFinMatiere++; if (balls[i].t == 1)
		 * typeFinAntiMatiere++; } if (typeFinAntiMatiere == 0 || typeFinMatiere
		 * == 0) setup();
		 */

		// TODO : Ajouter le serveur
		// myServer.write(balls[0].toString());
	

